package com.example.designsoft.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.designsoft.MyApplication;

/**
 * author : Gyk
 * time   : 2020/11/3
 * todo   :拉动翻页view
 */
public class PullPageView extends View {

    private Context context;
    private int width;
    private int height;
    private Paint linePaint;
    private Path path;
    private int currentY;
    private int currentX;
    private boolean isPull;
    private int changeX;


    public PullPageView(Context context) {
        super(context);
        initView();
    }

    public PullPageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PullPageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getHeight();
        currentY = height / 2;
        currentX = width / 4;
    }

    private void initView() {
        linePaint = new Paint();
        linePaint.setColor(Color.BLUE);
        path = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);
        drawPath(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getX() < currentX) {
                    isPull = true;
                } else {
                    isPull = false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                currentY = (int) event.getY();
                if (isPull) {
                    currentX = (int)event.getX();
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }

    private void drawLine(Canvas canvas) {
        RectF rectF = new RectF();
        rectF.set(0, 0, currentX, height);
        canvas.drawRect(rectF, linePaint);
    }

    private void drawPath(Canvas canvas) {
        path.lineTo(currentX, currentY - 150);
        path.cubicTo(currentX, currentY - 30, currentX + 60, (currentY) - 60, currentX + 80, currentY);
        path.cubicTo(currentX + 60, currentY + 60, currentX, currentY + 30, currentX, currentY + 150);
        canvas.drawPath(path, linePaint);
        path.reset();
    }

    private int measureWidth(int widhtMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(widhtMeasureSpec);
        int specSize = MeasureSpec.getSize(widhtMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = 200;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = 200;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
}
